<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/10/21
 * Time: 9:17
 */

namespace App\GraphQL\Type;

use App\User;
//use Rebing\GraphQL\Support\Facades\GraphQL;
use GraphQL\Type\Definition\Type;
use Rebing\GraphQL\Support\Type as GraphQLType;

class UsersType extends GraphQLType
{

    protected $attributes = [
        'name' => 'users',
        'description' => '用户',
        'model' => User::class,
    ];



    /*
      query {
          __type(name: "users") {
            name
            fields {
              name
              description
              type {
                name
                kind
                ofType {
                  name //jobsType
                }
              }
            }

          }
        }
     */
    /**
     * 定义返回的字段接口
     * @return array
     */
    public function fields(): array
    {
        return [
            'id' => [
                'type' => Type::nonNull(Type::int()),
                'description' => '用户id'
            ],

            'name' => [
                'type' => Type::string(),
                'description' => '用户名'
            ],

            'email' => [
                'type' => Type::string(),
                'description' => '邮箱'
            ],

            'job' => [
                'type' => Type::listOf(\GraphQL::type('jobsType')),
                'description' => '用户的工作'
            ]
        ];
    }


    //修改器， get 时候用
    protected function resolveEmailField($root, $args)
    {

        return strtolower($root->email);

    }

}
